home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / PowerMac / SpriteWorld / Examples / SpriteTest / SpriteTest.c < prev    next >
Encoding:
Text File  |  1994-04-25  |  19.9 KB  |  920 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. // SpriteTest.c
  3. //
  4. // Created: 8/14/91 at 1:53 AM
  5. // By: Tony Myles
  6. //
  7. // Copyright © 1991-94 Tony Myles, All rights reserved worldwide.
  8. ///--------------------------------------------------------------------------------------
  9.  
  10.  
  11. #ifndef __SWCOMMON__
  12. #include "SWCommonHeaders.h"
  13. #endif
  14.  
  15. #ifndef __DESK__
  16. #include <Desk.h>
  17. #endif
  18.  
  19. #ifndef __PACKAGES__
  20. #include <Packages.h>
  21. #endif
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __DIALOGS__
  28. #include <Dialogs.h>
  29. #endif
  30.  
  31. #ifndef __RESOURCES__
  32. #include <Resources.h>
  33. #endif
  34.  
  35. #ifndef __MEMORY__
  36. #include <Memory.h>
  37. #endif
  38.  
  39. #ifndef __EVENTS__
  40. #include <Events.h>
  41. #endif
  42.  
  43. #ifndef __OSEVENTS__
  44. #include <OSEvents.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51. #ifndef __SEGLOAD__
  52. #include <SegLoad.h>
  53. #endif
  54.  
  55. #ifndef __STANDARDFILE__
  56. #include <StandardFile.h>
  57. #endif
  58.  
  59. #ifndef __SPRITECOMPILER__
  60. #include "SpriteCompiler.h"
  61. #endif
  62.  
  63. #ifndef __SPRITEWORLD__
  64. #include "SpriteWorld.h"
  65. #endif
  66.  
  67. #ifndef __SPRITELAYER__
  68. #include "SpriteLayer.h"
  69. #endif
  70.  
  71. #ifndef __SPRITE__
  72. #include "Sprite.h"
  73. #endif
  74.  
  75. #ifndef __FRAME__
  76. #include "Frame.h"
  77. #endif
  78.  
  79. #ifndef __SPRITEWORLDUTILS__
  80. #include "SpriteWorldUtils.h"
  81. #endif
  82.  
  83. #include "GameUtils.h"
  84. #include "DebugUtils.h"
  85.  
  86. #include "BlitPixie.h"
  87. #include "SpriteTest.h"
  88.  
  89. RgnHandle gWorkRgn = NULL;
  90.  
  91.  
  92. ///--------------------------------------------------------------------------------------
  93. // CreateSpriteTest
  94. ///--------------------------------------------------------------------------------------
  95.  
  96. OSErr CreateSpriteTest(
  97.     SpriteTestPtr* spriteTestP,
  98.     CWindowPtr srcWindowP)
  99. {
  100.     OSErr err = noErr;
  101.     SpriteTestPtr tempSpriteTestP;
  102.     SpriteWorldPtr spriteWorldP;
  103.     SpriteLayerPtr spriteLayerP;
  104.     SpritePtr testSpriteArray[kNumberOfTestSprites];
  105.     SpritePtr titleSpriteP;
  106.     FramePtr titleFrameP;
  107.     PixPatHandle pixPatH;
  108.     long spriteNum;
  109.     short oldResRefNum, curResRefNum;
  110.     Rect worldRect;
  111.  
  112.     *spriteTestP = NULL;
  113.  
  114.     gWorkRgn = NewRgn();
  115.  
  116.     SetPort((GrafPtr)srcWindowP);
  117.     worldRect = srcWindowP->portRect;
  118.  
  119.     tempSpriteTestP = (SpriteTestPtr)NewPtrClear((Size)sizeof(SpriteTestRec));
  120.  
  121.     if (tempSpriteTestP == NULL)
  122.     {
  123.         err = MemError();
  124.     }
  125.  
  126.     if (err == noErr)
  127.     {
  128.         tempSpriteTestP->isTestRunning = true;
  129.  
  130.         err = SWEnterSpriteWorld();        
  131.     }
  132.  
  133.     if (err == noErr)
  134.     {
  135.             // create the sprite world
  136.         err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, NULL);
  137.     }
  138.  
  139.     if (err == noErr)
  140.     {
  141.         tempSpriteTestP->spriteWorldP = spriteWorldP;
  142.  
  143.             // create the sprite layer
  144.         err = SWCreateSpriteLayer(&spriteLayerP);
  145.     }
  146.  
  147.     if (err == noErr)
  148.     {
  149.         oldResRefNum = CurResFile();
  150.         curResRefNum = OpenResFile("\pSpriteTest Frames");
  151.         err = ResError();
  152.     }
  153.  
  154.     if (err == noErr)
  155.     {
  156.         UseResFile(curResRefNum);
  157.  
  158.         tempSpriteTestP->spriteLayerP = spriteLayerP;
  159.  
  160.         err = SWCreateSpriteFromCIconResource(
  161.             testSpriteArray,
  162.             NULL,
  163.             kTestCIconID,
  164.             kNumberOfTestFrames,
  165.             kCompiledFatMask);
  166.  
  167.         UseResFile(oldResRefNum);
  168.         CloseResFile(curResRefNum);
  169.     }
  170.  
  171.     if (err == noErr)
  172.     {
  173.         tempSpriteTestP->testSpriteArray[0] = testSpriteArray[0];
  174.  
  175.             // create our sprites
  176.         for (spriteNum = 1; spriteNum < kNumberOfTestSprites; spriteNum++)
  177.         {
  178.             err = SWCloneSprite(testSpriteArray[0], testSpriteArray + spriteNum, NULL);
  179.  
  180.             if (err == noErr)
  181.             {
  182.                 tempSpriteTestP->testSpriteArray[spriteNum] = testSpriteArray[spriteNum];
  183.             }
  184.             else
  185.             {
  186.                 break;
  187.             }
  188.         }
  189.     }
  190.  
  191.     if (err == noErr)
  192.     {
  193.         err = SWCreateSprite(&titleSpriteP, NULL, 1);
  194.     }
  195.  
  196.     if (err == noErr)
  197.     {
  198.         tempSpriteTestP->titleSpriteP = titleSpriteP;
  199.  
  200.             // create a frame for our sprite
  201.         err = SWCreateFrameFromPictResource(&titleFrameP, 128, 129, kCompiledFatMask);
  202.     }
  203.  
  204.     if (err == noErr)
  205.     {
  206.         tempSpriteTestP->titleFrameP = titleFrameP;
  207.         *spriteTestP = tempSpriteTestP;
  208.  
  209.         SWAddFrame(titleSpriteP, titleFrameP);
  210.  
  211.         for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
  212.         {
  213.             if (spriteNum == (kNumberOfTestSprites / 2))
  214.             {
  215.                 SWAddSprite(spriteLayerP, titleSpriteP);
  216.             }
  217.  
  218.                 // add the sprite in the layer
  219.             SWAddSprite(spriteLayerP, testSpriteArray[spriteNum]);
  220.         }
  221.  
  222.             // add the layer to the world
  223.         SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  224.  
  225.         SWLockSpriteWorld(spriteWorldP);
  226.  
  227.         SetupSpriteTest(tempSpriteTestP);
  228.  
  229.         SWSetPortToBackGround(spriteWorldP);
  230.  
  231.         if (SWHasColorQuickDraw() && ((**srcWindowP->portPixMap).pixelSize > 1))
  232.         {
  233.                 // fill the sprite world with a pretty pattern
  234.             pixPatH = GetPixPat(kBackDropPixPatID);
  235.  
  236.             if (pixPatH != NULL)
  237.             {
  238.                 FillCRect(&worldRect, pixPatH);
  239.                 DisposePixPat(pixPatH);
  240.             }
  241.             else
  242.             {
  243.                 #ifdef dangerousPattern
  244.                 FillRect(&worldRect, qd.ltGray);
  245.                 #else
  246.                 FillRect(&worldRect, &qd.ltGray);
  247.                 #endif
  248.             }
  249.         }
  250.         else
  251.         {
  252.             #ifdef dangerousPattern
  253.             FillRect(&worldRect, qd.ltGray);
  254.             #else
  255.             FillRect(&worldRect, &qd.ltGray);
  256.             #endif
  257.         }
  258.  
  259.         SetPort((GrafPtr)srcWindowP);
  260.     }
  261.  
  262.     if (err != noErr)
  263.     {
  264.         DisposeSpriteTest(tempSpriteTestP);
  265.     }
  266.     
  267.     return err;
  268. }
  269.  
  270.  
  271. void DisposeSpriteTest(
  272.     SpriteTestPtr spriteTestP)
  273. {
  274.     SpriteLayerPtr curLayerP;
  275.     SpritePtr curSpriteP;
  276.  
  277.     if (gWorkRgn != NULL)
  278.         DisposeRgn(gWorkRgn);
  279.  
  280.     if (spriteTestP != NULL)
  281.     {
  282.         if (spriteTestP->spriteWorldP != NULL)
  283.         {
  284.             SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  285.  
  286.                 // dispose of each sprite in the layer
  287.             while ((curLayerP = SWGetNextSpriteLayer(spriteTestP->spriteWorldP, NULL)) != NULL)
  288.             {
  289.                 while ((curSpriteP = SWGetNextSprite(curLayerP, NULL)) != NULL)
  290.                 {
  291.                     SWRemoveSprite(curLayerP, curSpriteP);
  292.                     SWDisposeSprite(curSpriteP);
  293.                 }
  294.     
  295.                 SWRemoveSpriteLayer(spriteTestP->spriteWorldP, curLayerP);
  296.                 SWDisposeSpriteLayer(curLayerP);
  297.             }
  298.         }
  299.  
  300.         if (spriteTestP->spriteLayerP != NULL)
  301.         {
  302.             SWDisposeSpriteLayer(spriteTestP->spriteLayerP);
  303.         }
  304.     
  305.         if (spriteTestP->spriteWorldP != NULL)
  306.         {
  307.             SWDisposeSpriteWorld(spriteTestP->spriteWorldP);
  308.         }
  309.  
  310.         DisposePtr((Ptr)spriteTestP);
  311.     }
  312.  
  313.     SWExitSpriteWorld();
  314. }
  315.  
  316.  
  317. void SetupSpriteTest(
  318.     SpriteTestPtr spriteTestP)
  319. {
  320.     register long spriteNum;
  321.     Rect moveBoundsRect;
  322.  
  323.     moveBoundsRect = spriteTestP->spriteWorldP->windowFrameP->frameRect;
  324.  
  325.         // set up the test sprites
  326.     for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
  327.     {
  328.         SetupTestSprite(spriteTestP->testSpriteArray[spriteNum], &moveBoundsRect,
  329.                 GetRandom(0, moveBoundsRect.right),
  330.                 GetRandom(0, moveBoundsRect.bottom));
  331.     }
  332.  
  333.         // set up the title sprite
  334.     SWSetSpriteLocation(spriteTestP->titleSpriteP,
  335.             (moveBoundsRect.right / 2) - (spriteTestP->titleFrameP->frameRect.right / 2),
  336.             (moveBoundsRect.bottom / 2) - (spriteTestP->titleFrameP->frameRect.bottom / 2));
  337.     SWSetSpriteMoveTime(spriteTestP->titleSpriteP, -1);
  338.  
  339.     spriteTestP->isCommandActive[kSpriteTestTitleCommand] = true;
  340.     spriteTestP->isCommandActive[kBouncingBallsCommand] = true;
  341.     spriteTestP->isCommandActive[kCollisionDetectionCommand] = false;
  342.     spriteTestP->isCommandActive[kCopyBitsTestCommand] = true;
  343. }
  344.  
  345.  
  346. ///--------------------------------------------------------------------------------------
  347. // SetupTestSprite
  348. ///--------------------------------------------------------------------------------------
  349.  
  350. short gMoveDelta = 0;
  351.  
  352. void SetupTestSprite(
  353.     SpritePtr testSpriteP,
  354.     Rect *moveBoundsRect,
  355.     short horizLocation,
  356.     short vertLocation)
  357. {
  358.     Rect tempBoundsRect;
  359.     short horizMoveDelta;
  360.     short vertMoveDelta;
  361.  
  362.     gMoveDelta += 1;
  363.  
  364.     tempBoundsRect = *moveBoundsRect;
  365.     horizMoveDelta = gMoveDelta;        //GetRandom(2, 6);
  366.     vertMoveDelta =    gMoveDelta;        //GetRandom(2, 6);
  367.  
  368.         // calculate the movement boundary rectangle
  369.     InsetRect(&tempBoundsRect, horizMoveDelta, vertMoveDelta);
  370.  
  371.     if (GetRandom(0, 1) == 1)
  372.     {
  373.         horizMoveDelta = -horizMoveDelta;
  374.     }
  375.  
  376.     if (GetRandom(0, 1) == 1)
  377.     {
  378.         vertMoveDelta = -vertMoveDelta;
  379.     }
  380.  
  381.         // set the sprite’s movement characteristics
  382.     SWSetSpriteMoveBounds(testSpriteP, &tempBoundsRect);
  383.     SWSetSpriteMoveDelta(testSpriteP, horizMoveDelta, vertMoveDelta);
  384.     SWSetSpriteMoveProc(testSpriteP, SWBounceSpriteMoveProc);
  385.     SWSetSpriteMoveTime(testSpriteP, kTestSpriteMoveTime);
  386.  
  387.     SWSetSpriteFrameTime(testSpriteP, kTestSpriteFrameTime);
  388.     SWSetSpriteFrameRange(testSpriteP, 0, kNumberOfTestFrames - 1);
  389.     SWSetSpriteFrameAdvance(testSpriteP, GetRandom(0, 1) ? -1 : 1);
  390.  
  391.     SWSetSpriteCollideProc(testSpriteP, SWBounceSpriteCollideProc);
  392.  
  393.         // set the sprite’s initial location
  394.     SWSetSpriteLocation(testSpriteP, horizLocation, vertLocation);
  395. }
  396.  
  397.  
  398. ///--------------------------------------------------------------------------------------
  399. // SWBounceSpriteCollideProc
  400. ///--------------------------------------------------------------------------------------
  401.  
  402. void SWBounceSpriteCollideProc(
  403.     SpritePtr srcSpriteP,
  404.     SpritePtr dstSpriteP,
  405.     Rect* sectRect)
  406. {
  407.     register short tempDelta;
  408.     Rect rgnRect;
  409.  
  410.         // If both sprites use the same collision routine (this one),ignore the second collision.
  411.     if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
  412.         ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
  413.         (srcSpriteP > dstSpriteP)))
  414.         return;
  415.  
  416.     rgnRect = (**srcSpriteP->curFrameP->maskRgn).rgnBBox;
  417.  
  418.         // move the mask region to the new sprite location
  419.     OffsetRgn(srcSpriteP->curFrameP->maskRgn,
  420.                 (srcSpriteP->destFrameRect.left - rgnRect.left) +
  421.                 srcSpriteP->curFrameP->offsetPoint.h,
  422.                 (srcSpriteP->destFrameRect.top - rgnRect.top) +
  423.                 srcSpriteP->curFrameP->offsetPoint.v);
  424.  
  425.     rgnRect = (**dstSpriteP->curFrameP->maskRgn).rgnBBox;
  426.  
  427.         // move the mask region to the new sprite location
  428.     OffsetRgn(dstSpriteP->curFrameP->maskRgn,
  429.                 (dstSpriteP->destFrameRect.left - rgnRect.left) +
  430.                 dstSpriteP->curFrameP->offsetPoint.h,
  431.                 (dstSpriteP->destFrameRect.top - rgnRect.top) +
  432.                 dstSpriteP->curFrameP->offsetPoint.v);
  433.  
  434.     SectRgn(srcSpriteP->curFrameP->maskRgn, dstSpriteP->curFrameP->maskRgn, gWorkRgn);
  435.  
  436.     if (!EmptyRgn(gWorkRgn))
  437.     {
  438.         if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
  439.         {
  440.             if (!((srcSpriteP->destFrameRect.left == sectRect->left) &&
  441.                 (srcSpriteP->destFrameRect.right == sectRect->right)))
  442.             {
  443.                 srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
  444.  
  445.                 SWOffsetSprite(srcSpriteP, srcSpriteP->horizMoveDelta, 0);
  446.             }
  447.  
  448.             if (!((srcSpriteP->destFrameRect.top == sectRect->top) &&
  449.                 (srcSpriteP->destFrameRect.bottom == sectRect->bottom)))
  450.             {
  451.                 srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  452.  
  453.                 SWOffsetSprite(srcSpriteP, 0, srcSpriteP->vertMoveDelta);
  454.             }
  455.  
  456.             return;
  457.         }
  458.  
  459.             // swap movement delta's
  460.         if (srcSpriteP < dstSpriteP)        // Only one time!
  461.         {
  462.                 // reverse spins.
  463.             srcSpriteP->frameAdvance = -srcSpriteP->frameAdvance;
  464.             dstSpriteP->frameAdvance = -dstSpriteP->frameAdvance;
  465.  
  466.                 // swap h/v delta's
  467.             tempDelta = srcSpriteP->horizMoveDelta;
  468.             srcSpriteP->horizMoveDelta = dstSpriteP->horizMoveDelta;
  469.             dstSpriteP->horizMoveDelta = tempDelta;
  470.  
  471.             tempDelta = srcSpriteP->vertMoveDelta;
  472.             srcSpriteP->vertMoveDelta = dstSpriteP->vertMoveDelta;
  473.             dstSpriteP->vertMoveDelta = tempDelta;
  474.         }
  475.     }
  476. }
  477.  
  478.  
  479. void RunSpriteTest(
  480.     SpriteTestPtr spriteTestP)
  481. {
  482.     if (spriteTestP->isTestRunning)
  483.     {
  484.         SWLockSpriteWorld(spriteTestP->spriteWorldP);
  485.  
  486.         if (spriteTestP->isCommandActive[kCollisionDetectionCommand])
  487.         {
  488.             SWCollideSpriteLayer(spriteTestP->spriteLayerP, spriteTestP->spriteLayerP);
  489.         }
  490.  
  491.         SWProcessSpriteWorld(spriteTestP->spriteWorldP);
  492.         SWAnimateSpriteWorld(spriteTestP->spriteWorldP);
  493.  
  494.         SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  495.     }
  496. }
  497.  
  498.  
  499. void UpdateSpriteTest(
  500.     SpriteTestPtr spriteTestP)
  501. {
  502.     SWLockSpriteWorld(spriteTestP->spriteWorldP);
  503.     SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
  504.     SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  505. }
  506.  
  507.  
  508. void HandleCreateSpriteCommand(
  509.     SpriteTestPtr spriteTestP)
  510. {
  511.     OSErr err;
  512.     SpritePtr testSpriteP;
  513.     Rect moveBoundsRect;
  514.     Point mouseLocation;
  515.  
  516.     err = SWCloneSprite(spriteTestP->testSpriteArray[0], &testSpriteP, NULL);
  517.  
  518.     if (err == noErr)
  519.     {
  520.         moveBoundsRect = spriteTestP->spriteWorldP->windowFrameP->frameRect;
  521.         GetMouse(&mouseLocation);
  522.  
  523.         SetupTestSprite(testSpriteP, &moveBoundsRect, mouseLocation.h, mouseLocation.v);
  524.  
  525.         SWAddSprite(spriteTestP->spriteLayerP, testSpriteP);
  526.     }
  527. }
  528.  
  529.  
  530. void HandleGetSpriteInfoCommand(
  531.     SpriteTestPtr spriteTestP)
  532. {
  533. #if MPW
  534. #pragma unused(spriteTestP)
  535. #endif
  536. }
  537.  
  538.  
  539. void HandleRunPauseCommand(
  540.     SpriteTestPtr spriteTestP)
  541. {
  542.     spriteTestP->isTestRunning = !spriteTestP->isTestRunning;
  543. }
  544.  
  545.  
  546. void HandleSpriteTestTitleCommand(
  547.     SpriteTestPtr spriteTestP)
  548. {
  549.     spriteTestP->isCommandActive[kSpriteTestTitleCommand] =
  550.         !spriteTestP->isCommandActive[kSpriteTestTitleCommand];
  551.  
  552.     if (spriteTestP->isCommandActive[kSpriteTestTitleCommand])
  553.     {
  554.         //SWAddSprite(spriteTestP->spriteLayerP, spriteTestP->titleSpriteP);
  555.         SWSetSpriteVisible(spriteTestP->titleSpriteP, true);
  556.     }
  557.     else
  558.     {
  559.             // erase the sprite (theres got to be an easier way!)
  560.         SWSetSpriteVisible(spriteTestP->titleSpriteP, false);
  561.         //RunSpriteTest(spriteTestP);
  562.  
  563.             // remove the sprite
  564.         //SWRemoveSprite(spriteTestP->spriteLayerP, spriteTestP->titleSpriteP);
  565.     }
  566. }
  567.  
  568.  
  569. void HandleBouncingBallsCommand(
  570.     SpriteTestPtr spriteTestP)
  571. {
  572.     SpritePtr ballSpriteP;
  573.     Boolean isCommandActive;
  574.     long ballNum;
  575.     
  576.     isCommandActive = spriteTestP->isCommandActive[kBouncingBallsCommand] =
  577.         !spriteTestP->isCommandActive[kBouncingBallsCommand];
  578.  
  579.     for (ballNum = 0; ballNum < kNumberOfTestSprites; ballNum++)
  580.     {
  581.         ballSpriteP = spriteTestP->testSpriteArray[ballNum];
  582.  
  583.         SWSetSpriteVisible(ballSpriteP, isCommandActive);
  584.     }
  585. }
  586.  
  587.  
  588. void CopyBitsSpeedTestCommand(
  589.     SpriteTestPtr spriteTestP)
  590. {
  591.     WindowPtr testWindowP = FrontWindow();
  592.     SpritePtr curSpriteP;
  593.     unsigned long frames, seconds;
  594.     long ticks;
  595.  
  596.     HideCursor();
  597.     HideMenuBar(testWindowP);
  598.  
  599.     SWLockSpriteWorld(spriteTestP->spriteWorldP);
  600.     SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
  601.  
  602.     curSpriteP = NULL;
  603.  
  604.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  605.     {
  606.         if (curSpriteP != spriteTestP->titleSpriteP)
  607.         {
  608.             SWSetSpriteMoveTime(curSpriteP, 0);
  609.             SWSetSpriteFrameTime(curSpriteP, 0);
  610.         }
  611.     }
  612.  
  613.     ticks = TickCount();
  614.  
  615.     for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
  616.     {
  617.         SWProcessSpriteWorld(spriteTestP->spriteWorldP);
  618.         SWAnimateSpriteWorld(spriteTestP->spriteWorldP);
  619.     }
  620.  
  621.     seconds = ((TickCount() - ticks) / 60);
  622.  
  623.     curSpriteP = NULL;
  624.  
  625.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  626.     {
  627.         if (curSpriteP != spriteTestP->titleSpriteP)
  628.         {
  629.             SWSetSpriteMoveTime(curSpriteP, kTestSpriteMoveTime);
  630.             SWSetSpriteFrameTime(curSpriteP, kTestSpriteFrameTime);
  631.         }
  632.     }
  633.  
  634.     SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  635.  
  636.     ShowCursor();
  637.     ShowMenuBar(testWindowP);
  638.  
  639.     DisplayPerformance(frames, seconds);
  640. }
  641.  
  642.  
  643. void BlitPixieSpeedTestCommand(
  644.     SpriteTestPtr spriteTestP)
  645. {
  646.     WindowPtr testWindowP = FrontWindow();
  647.     SpritePtr curSpriteP;
  648.     unsigned long frames, seconds;
  649.     long ticks;
  650.  
  651.     HideCursor();
  652.     HideMenuBar(testWindowP);
  653.  
  654.     SWLockSpriteWorld(spriteTestP->spriteWorldP);
  655.     SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
  656.  
  657.     curSpriteP = NULL;
  658.  
  659.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  660.     {
  661.         if (curSpriteP != spriteTestP->titleSpriteP)
  662.         {
  663.             SWSetSpriteMoveTime(curSpriteP, 0);
  664.             SWSetSpriteFrameTime(curSpriteP, 0);
  665.         }
  666.  
  667.         SWSetSpriteDrawProc(curSpriteP, BlitPixieMaskDrawProc);
  668.     }
  669.  
  670.     ticks = TickCount();
  671.  
  672. #if SW_PPC
  673.     for (frames = 0; (((*(long*)0x16A) - ticks) < kTestTime); frames++)
  674. #else
  675.     for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
  676. #endif
  677.     {
  678.         SWProcessSpriteWorld(spriteTestP->spriteWorldP);
  679.         SWBlastAnimateSpriteWorld(spriteTestP->spriteWorldP);
  680.     }
  681.  
  682.     seconds = ((TickCount() - ticks) / 60);
  683.  
  684.     curSpriteP = NULL;
  685.  
  686.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  687.     {
  688.         if (curSpriteP != spriteTestP->titleSpriteP)
  689.         {
  690.             SWSetSpriteMoveTime(curSpriteP, kTestSpriteMoveTime);
  691.             SWSetSpriteFrameTime(curSpriteP, kTestSpriteFrameTime);
  692.         }
  693.  
  694.         SWSetSpriteDrawProc(curSpriteP, SWStdDrawProc);
  695.     }
  696.  
  697.     SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  698.  
  699.     ShowCursor();
  700.     ShowMenuBar(testWindowP);
  701.  
  702.     DisplayPerformance(frames, seconds);
  703. }
  704.  
  705.  
  706. void SpriteTestCommand(
  707.     SpriteTestPtr spriteTestP,
  708.     CWindowPtr srcWindowP)
  709. {
  710.     short x, i;
  711.  
  712.     SetPort((GrafPtr)srcWindowP);
  713.     HideMenuBar((WindowPtr)srcWindowP);
  714.  
  715.     for (x = 0; (x < 1000) && !Button(); x++)
  716.     {
  717.         #ifdef dangerousPattern
  718.         FillRect(&srcWindowP->portRect, qd.black);
  719.         #else
  720.         FillRect(&srcWindowP->portRect, &qd.black);
  721.         #endif
  722.  
  723.         DisposeSpriteTest(spriteTestP);
  724.         CreateSpriteTest(&spriteTestP, srcWindowP);
  725.         SetupSpriteTest(spriteTestP);
  726.         UpdateSpriteTest(spriteTestP);
  727.  
  728.         for (i = 0; (i < 100) && !Button(); i++)
  729.         {
  730.             SystemTask();
  731.             RunSpriteTest(spriteTestP);
  732.         }
  733.     }
  734.  
  735.     ShowMenuBar((WindowPtr)srcWindowP);
  736. }
  737.  
  738.  
  739. void CompileSpriteCommand(
  740.     SpriteTestPtr spriteTestP)
  741. {
  742. #if MPW
  743. #pragma unused(spriteTestP)
  744. #endif
  745.     OSErr err;
  746.     StandardFileReply sfReply;
  747.     SFTypeList sfTypes;
  748.     short curResRefNum, oldResRefNum;
  749.     PixelCodeHdl newPixCodeH;
  750.     CWindowPtr testWindowP = (CWindowPtr)FrontWindow();
  751.     Boolean isFileOpen = false;
  752.     short pixCodeResID;
  753.     short colorIconIndex, numberOfColorIcons;
  754.     Str255 resName;
  755.     ResType tempType;
  756.     Handle tempIconH;
  757.  
  758.     StandardGetFile(NULL, -1, sfTypes, &sfReply);
  759.  
  760.     if (sfReply.sfGood)
  761.     {
  762.         oldResRefNum = CurResFile();
  763.         err = ResError();
  764.  
  765.         if (err == noErr)
  766.         {
  767.             curResRefNum = FSpOpenResFile(&sfReply.sfFile, fsRdWrPerm);
  768.             err = ResError();
  769.         }
  770.  
  771.         if (err == noErr)
  772.         {
  773.             isFileOpen = true;
  774.             UseResFile(curResRefNum);
  775.             err = ResError();
  776.         }
  777.  
  778.         if (err == noErr)
  779.         {
  780.             numberOfColorIcons = Count1Resources(kColorIconResType);
  781.             err = ResError();
  782.         }
  783.  
  784.         for (colorIconIndex = 1; (err == noErr) && (colorIconIndex <= numberOfColorIcons); colorIconIndex++)
  785.         {
  786.             tempIconH = Get1IndResource(kColorIconResType, colorIconIndex);
  787.  
  788.             if (tempIconH != NULL)
  789.             {
  790.                 GetResInfo(tempIconH, &pixCodeResID, &tempType, resName);
  791.                 err = ResError();
  792.             }
  793.             else
  794.             {
  795.                 err = ResError();
  796.  
  797.                 if (err == noErr)
  798.                     err = resNotFound;
  799.             }
  800.  
  801.             if (err == noErr)
  802.             {
  803.                 err = SWCompileColorIconResource(pixCodeResID, &newPixCodeH);
  804.             }
  805.  
  806.             if (err == noErr)
  807.             {
  808.                 err = SWSavePixelCodeResource(newPixCodeH, pixCodeResID);
  809.  
  810.                 DisposeHandle((Handle)newPixCodeH);
  811.             }
  812.             else
  813.             {
  814.                 DebugStrNum("\pSWCompileColorIconResource", err);
  815.             }
  816.         }
  817.  
  818.         if (isFileOpen)
  819.         {
  820.             UseResFile(oldResRefNum);
  821.             CloseResFile(curResRefNum);
  822.         }
  823.  
  824.         if (err != noErr)
  825.             DebugStrNum("\pCompileSpriteCommand", err);
  826.     }
  827. }
  828.  
  829.  
  830. void RunCompiledSpriteCommand(
  831.     SpriteTestPtr spriteTestP,
  832.     Boolean useQuickDraw)
  833. {
  834. #if MPW
  835. #pragma unused(useQuickDraw)
  836. #endif
  837.     WindowPtr testWindowP = FrontWindow();
  838.     SpritePtr curSpriteP;
  839.     unsigned long frames, seconds;
  840.     long ticks;
  841.  
  842.     HideCursor();
  843.     HideMenuBar(testWindowP);
  844.  
  845.     SWLockSpriteWorld(spriteTestP->spriteWorldP);
  846.     SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
  847.  
  848.     curSpriteP = NULL;
  849.  
  850.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  851.     {
  852.         if (curSpriteP != spriteTestP->titleSpriteP)
  853.         {
  854.             SWSetSpriteMoveTime(curSpriteP, 0);
  855.             SWSetSpriteFrameTime(curSpriteP, 0);
  856.             SWSetSpriteDrawProc(curSpriteP, CompiledSpriteDrawProc);
  857.         }
  858.         else
  859.             SWSetSpriteDrawProc(spriteTestP->titleSpriteP, BlitPixieMaskDrawProc);
  860.     }
  861.  
  862.  
  863.     #if !SW_PPC
  864.     FlushInstructionCache();
  865.     #endif
  866.  
  867.     ticks = TickCount();
  868.  
  869.     for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
  870.     {
  871.         SWProcessSpriteWorld(spriteTestP->spriteWorldP);
  872.         SWBlastAnimateSpriteWorld(spriteTestP->spriteWorldP);
  873.     }
  874.  
  875.     seconds = ((TickCount() - ticks) / 60);
  876.  
  877.  
  878.     curSpriteP = NULL;
  879.  
  880.     while ((curSpriteP = SWGetNextSprite(spriteTestP->spriteLayerP, curSpriteP)) != NULL)
  881.     {
  882.         if (curSpriteP != spriteTestP->titleSpriteP)
  883.         {
  884.             SWSetSpriteMoveTime(curSpriteP, kTestSpriteMoveTime);
  885.             SWSetSpriteFrameTime(curSpriteP, kTestSpriteFrameTime);
  886.             SWSetSpriteDrawProc(curSpriteP, SWStdDrawProc);
  887.         }
  888.         else
  889.             SWSetSpriteDrawProc(spriteTestP->titleSpriteP, SWStdDrawProc);
  890.     }
  891.  
  892.  
  893.     SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
  894.  
  895.     ShowCursor();
  896.     ShowMenuBar(testWindowP);
  897.  
  898.     DisplayPerformance(frames, seconds);
  899. }
  900.  
  901.  
  902. void DisplayPerformance(
  903.     long frames,
  904.     long seconds)
  905. {
  906.     Str255 framesString, secondsString, fpsString;
  907.     long fps;
  908.     
  909.     NumToString(frames, framesString);
  910.     NumToString(seconds, secondsString);
  911.  
  912.     fps = (seconds > 0) ? frames / seconds : frames;
  913.     NumToString(fps, fpsString);
  914.  
  915.     ParamText(framesString, secondsString, fpsString, "\p");
  916.     NoteAlert(kPerformanceAlertID, NULL);
  917. }
  918.  
  919.  
  920.